import Vue from 'vue'
import VueRouter from 'vue-router'
/* const Login = ()=>import('../pages/Login/Login.vue') */
Vue.use(VueRouter)

const routes = [
  {
    path: "/",
    redirect: "/index",
    meta: {
      title: "小U商城"
    }
  },
  {
    path: "/login",
    name: "login",
    component: () => import('../views/uLogin.vue'),
    meta: {
      title: "手机登录"
    }
  },
  {
    path: "/index",
    name: "index",
    component: () => import('../views/uIndex.vue'),
    meta: {
      title: "小U商城"
    }
  }, 
  {
    path: "/class",
    name: "class",
    component: () => import('../views/uClass.vue'),
    meta: {
      title: "商品分类"
    }
  }, 
  {
    path: "/cart",
    name: "cart",
    component: () => import('../views/uCart.vue'),
    meta: {
      title: "购物车"
    }
  }, 
  {
    path: "/mine",
    name: "mine",
    component: () => import('../views/uMine.vue'),
    meta: {
      title: "我的"
    }
  },
  {
    path: "/list",
    name: "list",
    component: () => import('../views/uList.vue'),
    meta: {
      title: "商品列表"
    }
  },
  {
    path: "/search",
    name: "search",
    component: () => import('../views/uSearch.vue'),
    meta: {
      title: "商品搜索"
    }
  },
  {
    path: "/detail",
    name: "detail",
    component: () => import('../views/uDetail.vue'),
    meta: {
      title: "商品详情"
    }
  },
  {
    path: "/order",
    name: "order",
    component: () => import('../views/uOrders.vue'),
    meta: {
      title: "确认订单"
    }
  },
  {
    path: "/address",
    name: "address",
    component: () => import('../views/uAddress.vue'),
    meta: {
      title: "地址管理"
    }
  },
  {
    path: "/increase",
    name: "increase",
    component: () => import('../views/uIncreaseAddress.vue'),
    meta: {
      title: "新增地址"
    }
  },
  {
    path: "/pay",
    name: "pay",
    component: () => import('../views/uPay.vue'),
    meta: {
      title: "支付页面"
    }
  },
  {
    path: "/register",
    name: "register",
    component: () => import('../views/uRegister.vue'),
    meta: {
      title: "用户注册"
    }
  },
]
const router = new VueRouter({
  routes
})
// 白名单路由path
const whitePaths = ['/index','/class','/login','/register']
// 全局前置导航守卫
router.beforeEach((to, from, next) => {
  // 1、判断是不是登陆权限的页面
      // 1、1如果不需要登陆，直接放行
      // 1、2如果需要登录检测
        // 1、2、1 检测本地存储中有无token
  if (whitePaths.includes(to.path)) {
    next()
  } else {
    // 从本地存储中提起user信息
    const user = JSON.parse(localStorage.getItem('user')) || {}
    if (user.token) {
      next()
    } else {
      /* console.log(111); */
      next('/login')
    }
  }
})

export default router
